package com.sohh.controller;

import com.sohh.entity.Users;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: some desc
 * @author: ruoan
 * @date: 2021/2/5 12:32
 */
@RestController
public class IndexController {
    @GetMapping("/index")
    public String index(){
        a();
        return "index";
    }

    public void a(){
        b();
    }
    public void b(){
        c();
    }
    public void c(){
        System.out.println("d");
    }


    @GetMapping("/hello")
    public String hello(){

        return "登录成功！！！";
    }

    @GetMapping("/hello2")
    public String hello2(){

        return "我不需要验证！！！";
    }


    @GetMapping("/hello3")
    public String hello3(){

        return "我需要访问权限！！！";
    }

    /**
     * 用于处理登录path到html的映射
     * @return
     */
    @GetMapping(value={"/login","/"})
    public ModelAndView login(){

        return new ModelAndView("login.html");
    }



    /**
     * 判断用户是否有某个角色的注解
     * @return
     */
    @GetMapping(value={"/hello4"})
    @Secured(value={"ROLE_yeah","ROLE_admin"})
    public String hello4(){

        return "own 此权限d";
    }



    /**
     * 判断用户是否有某个角色或权限的注解，可以结合hasAuthority(),hasAnyAuthority(),hasRole(),hasAnyRole()使用
     * @return
     */
    @GetMapping(value={"/hello5"})
    @PreAuthorize(value = "hasAuthority('happy2')")
    public String hello5(){

        return "own 此权限d----/hello5";
    }



    /**
     * 判断用户是否有某个角色或权限的注解，可以结合hasAuthority(),hasAnyAuthority(),hasRole(),hasAnyRole()使用
     * @return
     */
    @GetMapping(value={"/hello7"})
    @PostFilter("filterObject.username=='wangwu'")  //filterObject固定写法
    public List<Users> hello7(){
        List<Users> usersList = new ArrayList<>();
        usersList.add(new Users(3, "wangwu", "wangwu"));
        usersList.add(new Users(6, "wangwu2", "wangwu2"));
        usersList.add(new Users(9, "wangwu3", "wangwu3"));

        return usersList;
    }
}
